package controllers

import play.api._
import play.api.mvc._
import models.Message
import models.Message._

object Application extends Controller {
  
  def index = Action {
    Redirect(routes.Application.messages)
  }
  
  def messages=Action{

  	Ok(views.html.index(Message.all(),Message.messageForm))

  }

  def newMessage=Action{implicit request=>

  	messageForm.bindFromRequest.fold(

  		errors=>BadRequest(views.html.index(Message.all(),errors)),

  		label=>{
  			Message.create(label)
  			Redirect(routes.Application.messages)
  		}
  		)
  }

  def deleteMessage(id:Long)=Action{

			Message.delete(id)

			Redirect(routes.Application.messages)
  }
  
  def updateMessage(id:Long)=Action{implicit request=>

	messageForm.bindFromRequest.fold(

		errors=>BadRequest(views.html.index(Message.all(),errors)),
		label=>{
			Message.update(id,label)
			Redirect(routes.Application.messages)
		}
	  )
  }
  
}